/**
 * @class UI-条目组件-天体图标
 * @author DuskyHuang 圣子
 * @description 代表每个天体的图片名称的按钮。
*/

import { _decorator, Component, Sprite, Button, Node } from 'cc';
import { core, IItem } from 'cfw';
import { Res } from 'const';
import { UI, View } from 'notifications';
import { planetary, solar, constStars, deepGalaxy, visit, log, LogProxy } from 'proxies';
import { IIconData } from '../../celestial';
import { LabelI18n } from '../label-i18n';
const { ccclass, property } = _decorator;

@ccclass('cocosmos.celestial-icon')
export class CelestialIcon extends Component implements IItem<IIconData> {

    @property(Sprite) border: Sprite = null;
    @property(Sprite) icon: Sprite = null;
    @property(Sprite) invisible: Sprite = null;
    @property(Sprite) rank: Sprite = null;
    @property(LabelI18n) label: LabelI18n = null;
    @property(Button) button: Button = null;
    @property(Node) arrived: Node = null;

    private _data: Partial<IIconData> = {};

    protected onLoad(): void {
        core.on(UI.VISIBLE, this.resetVisible, this);
        core.on(View.Transform.VISIT_OVER, this.onVisitOver, this);
    }

    protected onDestroy(): void {
        core.unload(this);
    }

    public onDataUpdated(data: IIconData): void {
        const { into, name, label: en = name, tag } = data;
        if (this._data.into !== into) {
            const postfix = into === 'minor' ? 'satellite' : into === 'belts' ? 'dwarf' : into;
            this.border.spriteFrame = core.res.getSheetFrame(UI_BORDER, 'bdr_icon_' + postfix);
        }
        if (this._data.name !== name) {
            const icon = core.res.getSheetFrame(UI_ICON, 'icon_' + name);
            this.icon.spriteFrame = icon ?? core.res.getSheetFrame(UI_ICON, UI_ICON_DEFAULT);
        }
        this.label.string = 'celestial@' + en;
        this._data = data;
        this.node.name = name;
        this.resetTag(tag);
        this.resetVisible();
        this.resetArrived();
    }

    protected onVisitOver(): void {
        this.resetArrived();
    }

    protected resetArrived(): void {
        const isMe = this._data.name === visit.centerName && this._data.name != 'Sun';
        this.arrived.active = isMe;
    }

    protected resetTag(tag: string): void {
        if (!tag) { this.rank.node.active = false; }
        else {
            this.rank.spriteFrame = core.res.getSheetFrame(Res.Sheets.UI_ICON, tag);
            this.rank.node.active = true;
        }
    }

    protected resetVisible(into_cn?: into, shown?: boolean): void {
        let visible = true;
        switch (this._data.into) {
            // case 'sun': visible = solar.showSun; break;
            case 'planet': visible = solar.showPlanets; break;
            case 'dwarf': visible = solar.showDwarfs; break;
            case 'minor': visible = solar.showMinors; break;
            case 'belts': visible = solar.showBelts; break;
            case 'comet': visible = solar.showComets; break;
            case 'satellite': visible = planetary.showSatellites; break;
            case 'artificial': visible = planetary.showArtificial; break;
            case 'star': {
                if (this._name === 'Sun') visible = solar.showSun;
                else visible = constStars.showStars;
            } break;
            case 'constellation': visible = constStars.showConsts; break;
            case 'nebula': visible = deepGalaxy.showNebulas; break;
            case 'cluster': visible = deepGalaxy.showClusters; break;
            case 'galaxy': visible = deepGalaxy.showGalaxies; break;
            case 'blackhole': visible = deepGalaxy.showBlackHoles; break;
        }
        this.button.interactable = visible;
        this.invisible.enabled = !visible;
    }

    protected onClick(): void {
        const { into, name, pop } = this._data;
        if (pop === 'visit-cd') {
            core.notify(UI.Popups.VISIT_CD, name, into);
        } else {
            if (LogProxy.PRINT_VISIT) console.info('=>', UI.VISIT, name, into);
            core.notify(UI.VISIT, name, into);
        }
    }
}

const { Sheets: { UI_BORDER, UI_ICON, UI_ICON_DEFAULT } } = Res;
